package io.renren.modules.eightapp.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;

import io.renren.modules.eightapp.dto.EiComplainDTO;
import io.renren.modules.eightapp.dto.EiComplainInfoDTO;
import io.renren.modules.eightapp.entity.EiComplainInfoEntity;
import io.renren.modules.eightapp.excel.EiComplainInfoExcel;
import io.renren.modules.eightapp.service.EiComplainInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author Mark 1132115960@gmail.com
 * @since 1.0.0 2022-12-27
 */
@RestController
@RequestMapping("eightapp/eicomplaininfo")
@Api(tags="")
public class EiComplainInfoController {
    @Autowired
    private EiComplainInfoService eiComplainInfoService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @RequiresPermissions("eightapp:eicomplaininfo:page")
    public Result<PageData<EiComplainInfoDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<EiComplainInfoDTO> page = eiComplainInfoService.page(params);

        return new Result<PageData<EiComplainInfoDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("eightapp:eicomplaininfo:info")
    public Result<EiComplainInfoDTO> get(@PathVariable("id") Long id){
        EiComplainInfoDTO data = eiComplainInfoService.get(id);

        return new Result<EiComplainInfoDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("eightapp:eicomplaininfo:save")
    public Result save(@RequestBody EiComplainInfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        Timestamp time= new Timestamp(System.currentTimeMillis());
        dto.setUpdateTime(time);
        dto.setCreateTime(time);
        dto.setCreator("admin");

        eiComplainInfoService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("eightapp:eicomplaininfo:update")
    public Result update(@RequestBody EiComplainInfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        Timestamp time= new Timestamp(System.currentTimeMillis());
        dto.setUpdateTime(time);

        eiComplainInfoService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("eightapp:eicomplaininfo:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        eiComplainInfoService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("eightapp:eicomplaininfo:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<EiComplainInfoDTO> list = eiComplainInfoService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, EiComplainInfoExcel.class);
    }


    @PutMapping("/{id}")
    @ApiOperation("修改处理状态")
    public Result<Integer> updateStateById(@PathVariable("id") Integer id) {
        int i = eiComplainInfoService.updateStateById(id);
        return new Result<Integer>().ok(i);
    }

    @GetMapping("/getComplainListByownerId/{ownerId}")
    @ApiOperation("根据业主id获取投诉集合")
    public Result<List<EiComplainInfoDTO>> getComplainListByownerId(@PathVariable("ownerId") int ownerId) {
        List<EiComplainInfoDTO> list = eiComplainInfoService.getComplainListByownerId(ownerId);
        return new Result<List<EiComplainInfoDTO>>().ok(list);
    }

    @GetMapping("/getComplainByid/{id}")
    @ApiOperation("根据id搜索")
    public Result<EiComplainInfoDTO> getComplainById(@PathVariable("id") int id) {
        EiComplainInfoDTO complainById = eiComplainInfoService.getComplainById(id);
        return new Result<EiComplainInfoDTO>().ok(complainById);
    }


    @DeleteMapping("/deleteComplainById/{id}")
    @ApiOperation("根据id删除")
    public Result<Integer> deleteComplainById(@PathVariable("id") int id) {
        Integer i = eiComplainInfoService.deleteComplainById(id);
        return new Result<Integer>().ok(i);
    }

    @DeleteMapping("/deleteComplainList/{ids}")
    @ApiOperation("根据id批量删除")
    public Result<Integer> deleteComplainList(@PathVariable List<Integer> ids) {
        Integer i = eiComplainInfoService.deleteComplainList(ids);
        return new Result<Integer>().ok(i);
    }

    @GetMapping("/getStateListById/{ids}")
    @ApiOperation("根据id获取处理状态集合")
    public Result<Integer> getStateListById(@PathVariable List<Integer> ids) {
        Integer stateListById = eiComplainInfoService.getStateListById(ids);
        return new Result<Integer>().ok(stateListById);
    }


    @GetMapping("/getComplainList")
    @ApiOperation("获取所有投诉集合联查")
    public Result<List<EiComplainDTO>> getComplainList(){
        List<EiComplainDTO> listAll = eiComplainInfoService.getComplainList();
        return new Result<List<EiComplainDTO>>().ok(listAll);
    }
}